package org.example.Mapper;

import org.apache.ibatis.annotations.*;
import org.example.domain.ActivityRecord;
import org.example.domain.ApplyRecord;
import org.example.domain.User;
import java.util.Date;

@Mapper
public interface ApplyRecordMapper {

    @Insert("INSERT INTO applyRecord (userId,date,activityRecordIdOne,activityRecordIdTwo,activityRecordIdThree," +
            "activityRecordIdFour,activityRecordIdFive,activityRecordIdSix) VALUES (#{userId},#{date}," +
            "#{activityRecordIdOne},#{activityRecordIdTwo},#{activityRecordIdThree},#{activityRecordIdFour}" +
            ",#{activityRecordIdFive},#{activityRecordIdSix});")
    void addApplyRecord(@Param("userId") Integer userId,@Param("date") Date date,
                        @Param("activityRecordIdOne") Integer activityRecordIdOne,
                        @Param("activityRecordIdTwo") Integer activityRecordIdTwo,
                        @Param("activityRecordIdThree") Integer activityRecordIdThree,
                        @Param("activityRecordIdFour") Integer activityRecordIdFour,
                        @Param("activityRecordIdFive") Integer activityRecordIdFive,
                        @Param("activityRecordIdSix") Integer activityRecordIdSix);

    @Select("SELECT * FROM applyRecord WHERE status=0;")
    ApplyRecord[] getApplyRecords();

    @Update("UPDATE applyRecord SET status=2 WHERE id=#{id};")
    void accessApply(Integer id);

    @Update("UPDATE applyRecord SET status=1 WHERE id=#{id};")
    void refuseApply(Integer id);

    @Delete("DELETE FROM applyRecord WHERE userId=#{userId};")
    void deleteApplyRecordByUserId(Integer userId);

    @Select("SELECT * FROM applyRecord WHERE id=#{id} LIMIT 1;")
    ApplyRecord getApplyRecordById(Integer id);

    @Select("SELECT * FROM applyRecord WHERE userId=#{userId} ORDER BY date DESC LIMIT #{start},10")
    @Results({
            @Result(column = "date",property = "date"),//申请时间
            @Result(column = "status",property = "status"),//申请状态
            @Result(
                    column = "userId",
                    property = "user",
                    javaType= User.class,
                    one = @One(select="org.example.Mapper.UserMapper.getAllById")
            ),
            @Result(
                    column = "activityRecordIdOne,activityRecordIdOne,activityRecordIdTwo,activityRecordIdTwo," +
                            "activityRecordIdThree,activityRecordIdThree,activityRecordIdFour,activityRecordIdFour," +
                            "activityRecordIdFive,activityRecordIdFive,activityRecordIdSix,activityRecordIdSix",
                    property = "activityRecords",
                    javaType = ActivityRecord[].class,
                    many = @Many(select = "org.example.Mapper.ActivityRecordMapper.getActivityRecordsInIds")
            )
    })
    ApplyRecord[] getApplyRecordsByUserId(@Param("userId") Integer userId,@Param("start") Integer start);
}